import datetime
import time
import MSFile
import os
import threading
import hashlib
import Cookie

def hash(s): return hashlib.md5(s).hexdigest()
def cookie(env):
    if env.has_key('HTTP_COOKIE'): return Cookie.SimpleCookie(env['HTTP_COOKIE'])
    return Cookie.SimpleCookie()

class MSUser:
    def __init__(self, path, userNameHash):
        self.lock       = threading.Lock()
        self.hash       = userNameHash
        self.storage    = MSFile.File(path, self.hash)
        self.storage.infoname = path + self.hash + '.user'

    def open(self, Locked=False):
        if not Locked: self.lock.acquire()
        try:
            self.storage.open()
        finally:
            if not Locked: self.lock.release()
    def flush(self, Locked=False):
        if not Locked: self.lock.acquire()
        self.storage.lock()
        try:
            self.storage.setInfo( (1, self.storage.meta) )
        finally:
            self.storage.unlock()
            if not Locked: self.lock.release()
